package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import pojo.GuestDto;
import pojo.Manager;

public class CheckInDao {
	public CheckInDao(){
	}
	
	public int  updatRoom(String roomNum){
		ConnectDao connectDao = new ConnectDao();
		String sql = "UPDATE room SET status = '1' WHERE roomNum = '"+roomNum+"'";
		try{
			int i = connectDao.stat.executeUpdate(sql);
			connectDao.closeConnect();
			return i;
		}catch(SQLException e){
			e.printStackTrace();
			connectDao.closeConnect();
			return 0;
		}
	}
	
	public List<String> insertGuest(List<GuestDto> list){
		ConnectDao connectDao = new ConnectDao();
		List<String> indexList = new ArrayList<String>();
		int count = 0;
		for(int i=0;i<list.size();i++){
			GuestDto gue = list.get(i);
			String sql = "INSERT INTO guest (guestId,name,tel,sex,day,stayDay)VALUES('"+gue.id+"','"+gue.name+"','"+gue.tel+"','"+gue.sex+"','"+gue.day+"','"+gue.perDay+"')";
			String sql1 = "SELECT MAX(personId) FROM guest";
			try{
				connectDao.stat.executeUpdate(sql);
				ResultSet rs = connectDao.stat.executeQuery(sql1);
				while(rs.next()){
					indexList.add(rs.getString(1));
					break;
				}
			}catch(SQLException e){
				e.printStackTrace();
				connectDao.closeConnect();
				return null;
			}
		}
		connectDao.closeConnect();
		return indexList;
	}
	
	public String  insertOrder(List<GuestDto> list,String manageNum){
		ConnectDao connectDao = new ConnectDao();
		GuestDto guset = list.get(0);
		String sql = "INSERT INTO billOrder(prepay,total,num) VALUES('"+guset.perPay+"','"+guset.paid+"','"+manageNum+"')";
		
		String sql1 = "SELECT MAX(orderId) FROM billOrder";
		String index = null;
		try{
			int i = connectDao.stat.executeUpdate(sql);
			ResultSet rs = connectDao.stat.executeQuery(sql1);
			while(rs.next()){
				index =  rs.getString(1);
				break;
			}
			connectDao.closeConnect();
			return index;
		}catch(SQLException e){
			e.printStackTrace();
			connectDao.closeConnect();
			return null;
		}
	}
	
	public void insertLive(String orderId,List<String> list,String roomNum)
	{
		ConnectDao connectDao = new ConnectDao();
		for(int i=0;i<list.size();i++)
		{
			String personId = list.get(i);
			String sql = "INSERT INTO lives (orderId,personId,roomNum,status) VALUES('"+orderId+"','"+personId+"','"+roomNum+"','1')";
			try{
				connectDao.stat.executeUpdate(sql);
			}catch(SQLException e){
				e.printStackTrace();
				connectDao.closeConnect();
			}
		}
		connectDao.closeConnect();
	}
}
